<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\TiebaService;
use Illuminate\Http\Request;

class TiebaController extends BaseController
{
    //用户

    public function __construct(TiebaService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['is_follow'] = $request->post('is_follow', 0);
        $pas['is_recommend'] = $request->post('is_recommend', 0);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $res = $this->db->getList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //医生列表
    public function doctorList(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['is_follow'] = $request->post('is_follow', 0);
        $pas['is_recommend'] = $request->post('is_recommend', 0);
        $pas['utype'] = 1;
        $res = $this->db->getList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //医生发布
    public function doctorAdd(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['cnt'] = $request->post('cnt', '');
        $pas['img'] = $request->post('img', '');
        if (!$pas['uid']) {
            return $this->toJson(-2, '未登录');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '分类必须的');
        }
        if (!$pas['title'] || !$pas['cnt']) {
            return $this->toJson(-2, '标题或内容必须的');
        }
        $res = $this->db->doctorAdd($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //发布
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['cnt'] = $request->post('cnt', '');
        $pas['img'] = $request->post('img', '');
        $pas['read_cost'] = $request->post('read_cost', '');
        $pas['price'] = $request->post('price', '');
        $pas['title'] = $request->post('title', '');
        if (!$pas['uid']) {
            return $this->toJson(-2, '未登录');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '分类必须的');
        }
        if (!$pas['cnt']) {
            return $this->toJson(-2, '标题或内容必须的');
        }
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //帖子删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //评论列表
    public function replyList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $tiezi = $request->post('tiezi', 0);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('limit', 10);
        if (!$tiezi) {
            return $this->toJson(-2, '没有帖子');
        }
        $res = $this->db->replyList($tiezi, $pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //评论发布
    public function replyAdd(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        $pas['tiezi'] = $request->post('tiezi', 0);
        $pas['pid'] = $request->post('pid', 0);
        $pas['cnt'] = $request->post('cnt', '');
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['tiezi']) {
            return $this->toJson(-2, '帖子错误');
        }
        if (!$pas['cnt']) {
            return $this->toJson(-2, '没有内容');
        }
        $res = $this->db->replyAdd($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //评论点赞
    public function replyLikeAdd(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        $tiezi = $request->post('tiezi', 0);
        $plid = $request->post('id', 0);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$tiezi) {
            return $this->toJson(-2, '没有ID');
        }
        $res = $this->db->likeAdd($tiezi, $plid, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}
